package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.exception.GlobalException;
import cn.itsource.pojo.domain.CustomerCar;
import cn.itsource.mapper.CustomerCarMapper;
import cn.itsource.pojo.dto.CustomerCarDto;
import cn.itsource.service.ICustomerCarService;
import cn.itsource.utils.IdUtil;
import cn.itsource.utils.RConst;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 客户车辆 服务实现类
 * </p>
 *
 * @author tt
 * @since 2024-09-04
 */
@Service
public class CustomerCarServiceImpl extends ServiceImpl<CustomerCarMapper, CustomerCar> implements ICustomerCarService {

    @Override
    public Boolean saveCar(CustomerCarDto customerCarDto) {
        Long customerId = StpUtil.getLoginIdAsLong();
        CustomerCar car = this.getOne(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, customerId)
                .eq(CustomerCar::getCarPlate, customerCarDto.getCarPlate())
                .eq(CustomerCar::getDeleted, RConst.Num.INT_0));
        if(car==null){
            CustomerCar customerCar = new CustomerCar();
            customerCar.setId(IdUtil.getSnowFlackId());
            customerCar.setCustomerId(customerId);
            customerCar.setCarType(customerCarDto.getCarType());
            customerCar.setCarPlate(customerCarDto.getCarPlate());
            customerCar.setCreateTime(new Date());
            return this.save(customerCar);
        }
        throw new GlobalException("该"+ customerCarDto.getCarPlate() +"车牌号已添加");
    }

    @Override
    public List<CustomerCar> carList() {
        Long customerId = StpUtil.getLoginIdAsLong();
        List<CustomerCar> list = this.list(new LambdaQueryWrapper<CustomerCar>()
                .eq(CustomerCar::getCustomerId, customerId)
                .eq(CustomerCar::getDeleted, RConst.Num.INT_0));
        return list;
    }
}
